package com.blog.controller.backend;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blog.entity.ProductType;
import com.blog.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mars123
 * @since 2019-07-02
 */
@Controller("backendProductTypeController")
@RequestMapping("/backend/productType")
public class ProductTypeController {


    @Autowired
    private ProductTypeService productTypeService;

    @RequestMapping("/index")
    public String index(Model model, Integer curPage) {
        model.addAttribute("page", productTypeService.index(null, new Page<ProductType>(curPage == null ? 1 : curPage, 5)));
        return "backend/product_type/index";
    }

    @RequestMapping("/create")
    public String create(@ModelAttribute("productType") ProductType productType, HttpServletRequest request) {
        if (request.getMethod().equals("POST") && productTypeService.create(productType) != null) {
            return "redirect:/backend/productType/index";
        }
        return "backend/product_type/create";
    }

    @RequestMapping("/update")
    public String update(@ModelAttribute("productType") ProductType productType, HttpServletRequest request, Integer id, Model model) {
        if (request.getMethod().equals("POST") && productTypeService.update(productType) != null) {
            return "redirect:/backend/productType/index";
        }
        model.addAttribute("productType", productTypeService.view(id));
        return "backend/product_type/update";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        productTypeService.delete(id);
        return "redirect:/backend/productType/index";
    }

}

